package com.huarui.company.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.service.BaseService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.huarui.company.dao.ICertifyDao;
import com.huarui.company.dao.ICompanyDao;
import com.huarui.company.model.Certify;
import com.huarui.company.model.CertifyListModel;
import com.huarui.company.model.Company;
import com.huarui.notice.INoticeService;
import com.huarui.notice.Notice;
import com.huarui.permission.service.PermissionService;

public class CertifyService extends BaseService{

   private ICompanyDao companyDao;
   
   private PermissionService permissionService;
   
   
   private INoticeService noticeService;
   

	public INoticeService getNoticeService() {
	return noticeService;
}


public void setNoticeService(INoticeService noticeService) {
	this.noticeService = noticeService;}


	public void setPermissionService(PermissionService permissionService) {
	this.permissionService = permissionService;
}


	public void setCompanyDao(ICompanyDao companyDao) {
	this.companyDao = companyDao;
}


	private ICertifyDao getDao() {

		return (ICertifyDao) dao;
	}
	
	
	public Certify getCertify(Integer companyId,Integer requestRealType){
		
		
		return this.getDao().getCertify(companyId,requestRealType);
	}
	
	@SuppressWarnings("unchecked")
	public CertifyListModel getCertifies(CertifyListModel certifyListModel){
	
		Map map = new HashMap<String, String>();
		
		map.put("start", certifyListModel.getStart());
		
		map.put("pageSize", certifyListModel.getPageSize());
		
		certifyListModel.setTotal(this.getDao().countByConditions(certifyListModel));
	
		List<Certify> certifyList = (List)this.getDao().getCertifies(certifyListModel);

		certifyListModel.setResult(certifyList);

		return certifyListModel;
		
	}
	
	
	
	
	   public void pass(final Certify certify,final Integer userId,final String resultMemo,final Integer marketId){
		
		
		
		   transactionTemplate.execute(new TransactionCallback() {

				@Override
				public Object doInTransaction(TransactionStatus status) {
					
					
						   certify.setGmtAuthority(new Date());
						   certify.setResultMemo(resultMemo);
						   certify.setGmtAuthor(userId);	
						   certify.setStatus(2);
						   certify.setResultRealType(certify.getRequestRealType());
						   certify.setResultCompanyType(certify.getRequestCompanyType());
						   getDao().update(certify);
		
						   
						   Company company = (Company)companyDao.getById(certify.getCompanyId());			
						   
						   
						   if(certify.getResultRealType()==1){
							   
							   company.setCertifyingStatus(3);
							   
						   }						   
						   if(certify.getResultRealType()==2){
							   
							   
							   company.setCertifyingStatus(4);
						   }
						   
						   
						   company.setTaxCertificationPath(certify.getTaxCertificationPath());
						   company.setIsCertifying(0);
						   company.setMarketId(marketId);
						   company.setContactorIdPath(certify.getContactorIdPath());
						   company.setBusinessLicensePath(certify.getBusinessLicensePath());
						   company.setRealType(certify.getResultRealType());
						   company.setCompanyType(certify.getResultCompanyType());
						   companyDao.update(company);
						   //认证成功后赠送的免费套餐
								
						   List<Integer> serviceIds = permissionService.getServiceNewAuthCompany(company.getId(),1);
												 					   
						   if(serviceIds.size()==0){					   
							   permissionService.donateFreePackage2NewAuthCompany(company.getId(),1);
						   }				
						   String title =company.getName()+":认证通过";
						   
						   String content="感谢您使用本系统,您已经通过了我们的认证,请退出重新登录,您现在可以享受在线业务管理了";

						   if(certify.getRequestRealType().equals(1)){
							   
							   
							  content=company.getName()+":感谢您使用本系统,您已经通过了我们的个人认证,请退出重新登录,您就可以享受在线业务管理了,您也可以进行公司认证了,获得我们更多的服务";
							   
						   }else{
							   
							   content=company.getName()+":感谢您使用本系统,您已经通过了我们的公司认证,请退出重新登录,您就可以享受更多的在线业务管理了";
							   
						   }
						   
						
						   sendMessage(company,userId,title,content);
				
					return null;
					
				}
			});
		
	}
		
	   public void unpass(final Certify certify,final Integer userId,final String resultMemo){
		
		
		
		   transactionTemplate.execute(new TransactionCallback() {

				@Override
				public Object doInTransaction(TransactionStatus status) {
					   certify.setResultMemo(resultMemo);
					   certify.setGmtAuthority(new Date());
					   certify.setGmtAuthor(userId);	
					   certify.setStatus(3);
					   certify.setResultRealType(certify.getRequestRealType());
					   certify.setResultCompanyType(certify.getRequestCompanyType());
					   getDao().update(certify);
					   Company company = (Company)companyDao.getById(certify.getCompanyId());
					   if(certify.getResultRealType()==1){
						   
						   company.setCertifyingStatus(5);
						   
					   }						   
					   if(certify.getResultRealType()==2){
						   
						   
						   company.setCertifyingStatus(6);
					   }
					   
					   company.setIsCertifying(0);

					   companyDao.update(company);
					   
					   String title =company.getName()+":认证不通过";
					   
					   if(certify.getRequestRealType().equals(1)){}
					   
					   String content=company.getName()+":感谢您使用本系统,您的信息还不能通过我们的审核,请仔细检查您上次的信息,然后再次认证";
					   sendMessage(company,userId,title,content);
					   
					return status;
					
				}
			});
		
	}  
	   
	   

	   
	   public void updateAll(final Certify certify, final Company company){
			
			
			
		   transactionTemplate.execute(new TransactionCallback() {

				@Override
				public Object doInTransaction(TransactionStatus status) {
					
					
					 getDao().update(certify);

					 companyDao.update(company);

					return status;
					
				}
			});
		
	}
	   
	   public void createAll(final Certify certify, final Company company){
			
			
			
		   transactionTemplate.execute(new TransactionCallback() {

				@Override
				public Object doInTransaction(TransactionStatus status) {
					
					
					 getDao().create(certify);
					 companyDao.update(company);

					return status;
					
				}
			});
		
	}  

	   
	   public void sendMessage(Company company,Integer userId,String title,String content){
		   
									
					Notice notice = new Notice();
					
					notice.setNoticeType(2);
					notice.setSenderId(userId);
					notice.setReceiverId(company.getId());
					notice.setReceiveType(1);
					notice.setTitle(title);
					notice.setContent(content);					
					noticeService.send(notice, 37);		   
		   
	   }
	   
	
}
